<?php

namespace App\Http\Controllers\Backstage\Resources;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Http\Request;

//资源模块
class ResourcesController extends Controller
{
    //获取资源
    public function getResources(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        //翻页
        $pagenNum = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        if ($pageSize > 50) {
            return false;
        }
        $page = $pagenNum - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
        }
        $keywords="";
        if($request->query('keywords')){
            $keywords=$request->query('keywords');
        }
        $count = DB::table('resources')->where('resources_title','like','%'.$keywords.'%')->count();
        $info = DB::table('resources')
            ->where('resources_title','like','%'.$keywords.'%')
            ->orderByDesc("resources_create_time")
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['resources_create_time'] = date('Y-m-d', $value['resources_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        $data['count'] = $count;
        return $data;
    }
    //获取资源详情
    public function getResourcesDetail(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['resources_id'] = (int) $request->query('resources_id');
        $info = DB::table('resources')
            ->where($map)
            ->first();
        if ($info) {
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }
    //新增资源
    public function addResources(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $date['resources_title'] = $request->input("resources_title");
        $date['resources_catid']=(int) $request->input('resources_catid');
        $date['resources_introduce'] = $request->input("resources_introduce");
        $date['resources_link'] = $request->input("resources_link");
        $date['resources_create_time'] = time();
        $msg = DB::table('resources')->insert($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //修改资源
    public function upResources(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['resources_id'] = (int) $request->input('resources_id');
        $date['resources_title'] = $request->input("resources_title");
        $date['resources_catid']=(int) $request->input('resources_catid');
        $date['resources_introduce'] = $request->input("resources_introduce");
        $date['resources_link'] = $request->input("resources_link");
        $date['resources_create_time'] = time();
        $msg = DB::table('resources')->where($map)->update($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //删除资源
    public function delResources(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['resources_id'] = (int) $request->query('resources_id');
        $msg = DB::table('resources')->where($map)->delete();
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
